Lex (or Flex)
-
Purpose: Lexical analysis
-
It reads raw input text and converts it into tokens (identifiers, keywords, numbers, symbols, etc.).
-
You describe token patterns using regular expressions.
-
Output: a scanner function (usually
yylex) that returns tokens to the parser. -
Example tasks:
-
Recognize keywords like if, while
-
Identify numbers, strings, operators
-
Skip whitespace and comments
-
Yacc (or Bison)
-
Purpose: Syntax analysis
-
It takes tokens from Lex and checks whether they follow the grammar of the language.
-
You describe the grammar using context-free grammar rules.
-
It builds a parse tree or triggers semantic actions.
-
Example tasks:
-
Enforce operator precedence
-
Validate expression structure
-
Drive AST construction or code generation
-